package com.CampusLife.controller;
import java.awt.*;
import	java.util.List;


import com.CampusLife.bean.*;
import com.CampusLife.service.IAdminService;
import com.CampusLife.service.IRoleService;
import com.CampusLife.service.IUerService;
import com.CampusLife.until.Encrypt;
import com.CampusLife.until.ImagesUpload;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private IAdminService iAdminService;
    @Resource
    private IRoleService iRoleService;

    /**
     * 跳转到后台登录页
     */
    @RequestMapping("/login")
    public String admin_login(){
        return "admin/login";
    }

    /**
     *
     * @param mname
     * @param password
     * 管理员登录验证
     */
    @RequestMapping("/login_verify")
    @ResponseBody
    public  int login_verify(String mname,String password,HttpSession httpSession,Model model){
        //给页面传过来的密码加密
        String ps=Encrypt.MD5(password)+"zhj";
        //再次加密防止密码外泄
        String  ps1=Encrypt.MD5(ps);
       Admin admin=iAdminService.login(mname,ps1);
        //如果查询结果不为空，则证明该用户合法，否则该用户不合法
        if (admin!=null){
            //将管理员登录名添加到session中，作为拦击器是否拦截页面请求
           httpSession.setAttribute("mname",admin.getMname());
           httpSession.setAttribute("mid",admin.getMid());

            int id=(int) httpSession.getAttribute("mid");



            int rid=admin.getR_id();
            return rid;
        /**    switch(rid)
            {
                case 1:  model.addAttribute("mname1",mname1); return "admin/index";
                case 2: return "redirect:/admin/admin2";
                case 3:return "redirect:/admin/admin1";
            }
        }else {
            return "redirect:/admin/login";
        }
        return "redirect:/admin/login";
    }**/
        }else {
        return 0;}
    }
    @RequestMapping("/admin1")
    public String menory(Model model,HttpSession httpSession) {
        List<Menu2> shiwuzhaoling=iRoleService.getshiwuzhaoling();
        model.addAttribute("shiwuzhaoling",shiwuzhaoling);
        List<Menu2> xianzhi=iRoleService.getxianzhi();
        model.addAttribute("xianzhi",xianzhi);
        String mname1=(String) httpSession.getAttribute("mname");
        model.addAttribute("mname1", mname1);
        return "admin/index2";
    }

    @RequestMapping("/admin2")
    public String menory1(Model model,HttpSession httpSession) {
        List<Menu2> lunbo=iRoleService.getlunbo();
        model.addAttribute("lunbo",lunbo);
        List<Menu2> xioayuantongzhi=iRoleService.getxioayuantongzhi();
        model.addAttribute("xioayuantongzhi",xioayuantongzhi);
        String mname1=(String) httpSession.getAttribute("mname");
        model.addAttribute("mname1", mname1);
        return "admin/index1";
    }
    /**
     * 页面传递过来用户密码password 修改过的密码password1  确认密码password2
     */
    @RequestMapping("/alter_ps")
    @ResponseBody
    public  int alterps(String password, String password1,String password2, HttpSession httpSession)throws Exception {
        String n = Encrypt.MD5(password) + "zhj";
        String ps = Encrypt.MD5(n);
        String mname=(String) httpSession.getAttribute("mname");
        int mid=(int) httpSession.getAttribute("mid");
        Admin admin = iAdminService.login(mname, ps);
           if(admin== null){
               return 1;
           }
           else if (admin!= null&&password1.equals(password2)) {
                    String m = Encrypt.MD5(password1) + "zhj";
                    String ps1 = Encrypt.MD5(m);
                    Admin admin1= new Admin();
                    System.out.println(ps1);
                    admin1.setPassword(ps1);
                    admin1.setMid(mid);
                    iAdminService.UpdatePassword(admin1);

                    return 0;
                } else {
                    return -500;
                }

         }

    /**
     * 跳转到前台首页
     */
    @RequestMapping("/home")
    public String home() {
        return "/home.jsp";
    }
    /**
     * 跳转到修改密码页面
     */
    @RequestMapping("/updatepassword")
   public String  updatepassword(){
        return "admin/password-updata";
   }
    /**
     * 跳转到后台首页面
     */
    @RequestMapping("index")
    public String index(HttpSession httpSession,Model model){
        String mname1=(String) httpSession.getAttribute("mname");
        model.addAttribute("mname1",mname1);
        return "admin/index";
    }
    /**
     * 管理员退出登录
     */
    @RequestMapping("/out")
    public String loginout(HttpSession httpSession){
        httpSession.invalidate();
        return "admin/login";
    }

    /**
     * 进入管理员管理页面
     *
     * @return
     */
    @RequestMapping("/inAdd_Admin")

    public String inAdmin(Model model){
      List<Role> roles= iRoleService.getRole();
      model.addAttribute("roles",roles);
        return "admin/addAdmin";
    }
    /**
     * 查询所有管理员信息
     */
    @RequestMapping("/selectAll")

    public String selectAll(Model model){
      List<Admin> admins= iAdminService.queryAll();
      model.addAttribute("admins", admins);
      return "admin/admin";
    }

    /**
     * 删除管理员
     */
    @RequestMapping("/delete")
    public  String delete(int mid){
        iAdminService.delete(mid);
        return "redirect:/admin/selectAll";
    }
    /**
     * 重置管理员密码
     */
    @RequestMapping("/updateAdminPassword")
    public  String updateAdminPassword(int mid){
         Admin admin=new Admin();
         admin.setPassword("801c2cdec4c852afc373f0e01159506a");
         admin.setMid(mid);
         iAdminService.UpdateAdminPassword(admin);
        return "redirect:/admin/selectAll";
    }

    /**
     * 添加管理人员
     *
     * @return
     */

    @RequestMapping("/AddAdmin")
    public  String AddAdmin(@RequestParam("file") MultipartFile file, String mname, String describe_txt, String phone, Integer r_id)throws Exception{
        ImagesUpload imagesUpload =new ImagesUpload();
        String photo1= imagesUpload.Upload(file);
        Admin admins=new Admin();
        admins.setPassword("801c2cdec4c852afc373f0e01159506a");
        admins.setDescribe_txt(describe_txt);
        admins.setMname(mname);
        admins.setPhone(phone);
        admins.setPhoto(photo1);
        admins.setR_id(r_id);
        iAdminService.add(admins);
        return "redirect:/admin/selectAll";
    }


}
